/*
 *@本JS功能：镜像分配详情
 *@本js作者：杨锡君
 *@编写时间：2018年06月25日
 */
$(function() {
    var Alert1 = new Alert();
    var confirm1 = new Confirm();
    var loading1 = new Loading();
    loading1.init();
    var $departNames = $("#departNames");
    var $tableBox = $(".tableBox");
    var th = "<tr>" +
        "<th class=\"col-lg-1\"><input type=\"checkbox\" name=\"controlAll\" id=\"controlAll\" class=\"allCheck\"></th>" +
        "<th class=\"col-lg-4\">章节名称</th>" +
        "<th class=\"col-lg-2\">镜像/集群名称</th>" +
        "<th class=\"col-lg-1\">类型</th>" +
        "<th class=\"col-lg-3\">配置上限</th>" +
        "<th class=\"col-lg-1\">镜像来源</th>" +
        "</tr>";

    function tabelModel(opt) {
        var str = '-';
        var arr = [];
        if(opt.flavor != null && opt.flavor != undefined &&opt.flavor != ''){
            if(opt.imageType == "单节点"){
                arr = opt.flavor.split(",");
                str = "CPU上限 "+ arr[0] +"核 内存上限 "+ arr[1] +"G";
            }else{
                arr = opt.flavor.split(",");
                str = "主节点：CPU:"+ arr[0] +"核 内存"+ arr[1] +"G 从节点1：CPU:"+ arr[2] +"核 内存"+ arr[3] +"G 从节点2：CPU:"+ arr[4] +"核 内存"+ arr[5]+"G";
            }
        }

        return "<tr data-num=\"0\">" +
            "<td class=\"col-lg-1\"><input data-id=" + opt.chapterId + " value=\"0\" name=\"selecteds\" type=\"checkbox\" class=\"oneCheck\" /></td>" +
            "<td class=\"col-lg-2\" title=" + opt.chapterName + ">" + opt.chapterName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.baseImageName + ">" + opt.baseImageName + "</td>" +
            "<td class=\"col-lg-2\" title=" + opt.imageType + ">" + opt.imageType + "</td>" +
            "<td class=\"col-lg-2\" title='"+ str +"' >"+ str +"</td>" +
            "<td class=\"col-lg-2\" title=" + opt.imageSource + ">" + opt.imageSource + "</td>" +
            "</tr>";
    }
    var classMana = {
        init: function() {
            this.bindEvent();
        },
        bindEvent: function() {
            var self = this;
            $(".courseName").html(result.paramData.courseName);
            var json = {
                "courseId":result.paramData.courseId
            };
            self.tabelList(json);
            $(".addClass").off("click").on("click", function() {
                self.allocateEvent();
            });
            $(".delClass").off("click").on("click", function() {
                self.delListEvent();
            });
            $(".looktg").off("click").click(function () {
                var json = JSON.stringify({
                    "pageName": $(this).attr("data-url"),
                    "paramData": {
                        "some": result.paramData.some
                    }
                });
                window.location.href = "skipPage?jsonStr=" + json;
            });
        },
        getImage:function (mark) {
            var self = this;
            var flag = true;
            if(mark != undefined){
                flag = mark;
            }
            $.ajax({
                type: "get",
                url: "manage/image/list",
                data:{"isInitial":flag},
                async: true,
                success: function(msg) {
                    if(msg.code == 200) {
                        $("#oneImage").empty();
                        if(msg.data.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.data, function(opt) {
                                tabel += "<option value='"+ opt.id +"'>"+ opt.name +"</option>";
                            });
                            $("#oneImage").append(tabel);
                        }
                    } else {
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                        return false;
                    }

                }
            });
        },
        getAllImage:function () {
            var self = this;
            $.ajax({
                type: "get",
                url: "manage/cluster/list",
                async: true,
                success: function(msg) {
                    if(msg.code == 200) {
                        $("#images").empty();
                        if(msg.data.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.data, function(opt) {
                                tabel += "<option value='"+ opt.id +"'>"+ opt.name +"</option>";
                            });
                            $("#images").append(tabel);
                        }
                    } else {
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                        return false;
                    }

                }
            });
        },
        allocateEvent:function () {
            var self = this;
            if($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要分配镜像的章节";
                Alert1.init();
                Alert1.show();
            } else {
                $(".popup,.mb").show();
                $(".come,.imageBox").show();
                self.getImage();
                $(".imagesBox").hide();
                $(".type").removeClass("radioCheck");
                $(".type").eq(0).addClass("radioCheck");
                $(".types").removeClass("radioChecks");
                $(".types").eq(0).addClass("radioChecks");
                $(".type").off("click").click(function () {
                    $(this).addClass("radioCheck");
                    $(this).siblings("span").removeClass("radioCheck");
                    if($(this).attr("data-value") == 1){
                        $(".come,.imageBox").show();
                        $(".imagesBox").hide();
                        $(".types").removeClass("radioChecks");
                        $(".types").eq(0).addClass("radioChecks");
                        $("#oneConfig").val('2,2');
                        self.getImage(true);
                    }else{
                        $(".come,.imageBox").hide();
                        $(".imagesBox").show();
                        $("#imagesFir").val('2,2');
                        $("#imagesSec").val('2,2');
                        $("#imagesThi").val('2,2');
                        self.getAllImage();
                    }
                });
                $(".types").off("click").click(function () {
                    $(this).addClass("radioChecks");
                    $(this).siblings("span").removeClass("radioChecks");
                    if($(this).attr("data-value") == 1){
                        self.getImage(true);
                    }else{
                        self.getImage(false);
                    }
                });
                // 取消 x
                $(".exportCancle").off("click").click(function () {
                    $(".popup,.mb").hide();
                });
                $(".popup_img").off("click").click(function () {
                    $(".popup,.mb").hide();
                });
                // 提交
                $(".exportBtn").off("click").click(function () {

                    var list = [];
                    for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                        list.push(Number($(".oneCheck[value=1]").eq(i).attr("data-id")));
                    }
                    var flag = true;
                    var json = {"chapterIds":list};
                    if($(".radioCheck").attr("data-value") == 1){
                        json.courseId = result.paramData.courseId;
                        json.cluster = false;
                        json.imageId = Number($("#oneImage").val());
                        json.flavor = $("#oneConfig").val();
                        if($("#oneImage").val() == null){
                            Alert1.content = "请正确选择镜像";
                            Alert1.init();
                            Alert1.show();
                            flag = false;
                            return false;
                        }
                    }else{
                        json.courseId = result.paramData.courseId;
                        json.cluster = true;
                        json.clusterId = Number($("#images").val());
                        json.flavor = $("#imagesFir").val()+","+$("#imagesSec").val()+","+$("#imagesThi").val();
                        if($("#images").val() == null){
                            Alert1.content = "请正确选择集群";
                            Alert1.init();
                            Alert1.show();
                            flag = false;
                            return false;
                        }
                    }
                    if(flag){
                        loading1.show();
                        json = JSON.stringify(json);
                        $.ajax({
                            type: "post",
                            url: "manage/image/allocate/confirm",
                            data: json,
                            dataType:"JSON",
                            contentType:'application/json',
                            async: true,
                            success: function(msg) {
                                loading1.hide();
                                if(msg.code == 200) {
                                    Alert1.content = "分配成功";
                                    Alert1.success = function () {
                                        window.location.reload(true);
                                    };
                                    Alert1.init();
                                    Alert1.show();
                                } else {
                                    Alert1.content = msg.message;
                                    Alert1.init();
                                    Alert1.show();
                                    return false;
                                }

                            },
                            error:function () {
                                loading1.hide();
                            }
                        });
                    }

                });
            }
        },
        tabelList:function (jsons) {
            var self = this;
            loading1.show();
            // json = JSON.stringify(jsons);
            $.ajax({
                type: "get",
                url: "manage/image/allocate/query",
                data: jsons,
                async: true,
                success: function(msg) {
                    loading1.hide();
                    // msg = JSON.parse(msg);
                    if(msg.code == 200) {
                        $tableBox.empty();
                        if(msg.data.length != 0) {
                            //列表
                            var tabel = "";
                            $.map(msg.data, function(opt) {
                                tabel += tabelModel(opt);
                            });
                            $tableBox.append(th+tabel);
                            self.allCheckEvent();
                        } else {
                            var trImg = "<tr><td colspan='6'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td></tr>";
                            $tableBox.append(th+trImg)
                        }
                    } else {
                        Alert1.content = msg.message;
                        Alert1.init();
                        Alert1.show();
                        return false;
                    }

                }
            });
        },
        delListEvent: function() {
            if($(".oneCheck[value=1]").length == 0) {
                Alert1.content = "请选择要解除镜像的章节";
                Alert1.init();
                Alert1.show();
            } else {
                var list = [];
                for(var i = 0; i < $(".oneCheck[value=1]").length; i++) {
                    list.push(Number($(".oneCheck[value=1]").eq(i).attr("data-id")));
                }
                confirm1.content = "确定解除选中的章节的镜像吗?";
                confirm1.success = function() {
                    var jsonStr = {"chapterIds":list};
                    jsonStr.courseId = result.paramData.courseId;
                    jsonStr = JSON.stringify(jsonStr);
                    loading1.show();
                    $.ajax({
                        type: "post",
                        url: "manage/image/allocate/cancel",
                        async: true,
                        dataType:'JSON',
                        contentType:'application/json',
                        data: jsonStr,
                        success: function(msg) {
                            loading1.hide();
                            if(msg.code == 200){
                                Alert1.content = "解除成功";
                                Alert1.success = function () {
                                    var json = JSON.stringify({
                                        "pageName":"imageAllocateDetail",
                                        "paramData":{
                                            "courseName":result.paramData.courseName,
                                            "courseId":result.paramData.courseId
                                        }
                                    });
                                    window.location.href = "skipPage?jsonStr="+json;
                                };
                                Alert1.init();
                                Alert1.show();
                            }else{
                                Alert1.content = msg.message;
                                Alert1.init();
                                Alert1.show();
                            }

                        },
                        error: function(err) {

                        }

                    });
                };
                confirm1.init();
                confirm1.show();

            }
        },
        allCheckEvent: function() {
            //全选
            allCheck.init("controlAll", "oneCheck");
        }
    };
    classMana.init();
});